package com.ruoyi.web.controller.app.service;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.exception.base.XException;
import com.ruoyi.sj.domain.Follow;
import com.ruoyi.sj.mapper.FollowMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @ClassName MyFollowService
 * @Description TODO
 * @Author tyc@yanyu
 * @Date 2018/12/10 0010 16:17
 **/
@Service
public class AppFollowService {
    @Autowired
    private FollowMapper followMapper;

    /**
     * 关注
     * @param memberId
     * @param otherId
     * @return
     */
    public Object addMyFollow(String memberId, String otherId) {
        if (StringUtils.isBlank(memberId)){
            throw new XException("关注人ID不能为空");
        }
        if (StringUtils.isBlank(otherId)){
            throw new XException("被关注人ID不能为空");
        }
        Follow follow = new Follow();
        follow.setMemberId(Long.valueOf(memberId)); //关注人ID
        follow.setOtherId(Long.valueOf(otherId)); //被关注人ID
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        follow.setCreateDate(sdf.format(new Date()));  //关注时间
        follow.setStatus(2); //状态为已关注
        return followMapper.insertFollow(follow);
    }

    /**
     * 关注列表
     * @param memberId
     * @return
     */
    public List<Follow> queryFollowList(String memberId) {
        return followMapper.queryList(memberId);
    }

    /**
     * 取消关注
     * @param otherId
     * @return
     */
    public int updateStatus(String otherId) {
        if (StringUtils.isBlank(otherId)){
            throw new XException("被关注人ID不能为空");
        }
        return followMapper.updateById(otherId);
    }

    public Long countNum(String otherId) {
        return followMapper.countNum(otherId);
    }
}
